
//页面加载获取树形菜单数据
$(function () {
    $.post("/sysRole/getMenuTreeByRoleId",{roleId:$("#roleId").val()},function(result){
        if(result){
            if (result['status']===0){
                initZtree(result['data']);
            }else {
                layer.alert(result['msg']);
            }
        }else{
            layer.alert('系统异常');
        }
    });
});

//初始化树形菜单
function initZtree(treeData){
    var setting = {
        data:{
            key: {
                name: "menuName",
                children:"childMenu"
            }
        },
        callback: {
            onClick: zTreeOnClick
        }
    };
    zTreeObj= $.fn.zTree.init($("#ztreelist"), setting, treeData);
}
//节点单击加载当前节点所有的操作权限（已有的操作勾选上）
var addAuthSmId="";
function zTreeOnClick(event, treeId, treeNode) {
    addAuthSmId=treeNode.smId;
    $('#data_table').DataTable({
        info : true, //显示表格左下角信息
        ordering: false,//关闭表格默认排序
        paging: true, //分页
        processing:true, //设置为true,就会有表格加载时的提示
        searching: false, //关闭本地搜索
        serverSide: true,//如果是服务器方式，必须要设置为true
        destroy: true,
        //使用ajax异步请求
        ajax:{
            dataSrc: function (json) {
                json.draw = json.data.draw;
                json.recordsTotal = json.data.recordsTotal;
                json.recordsFiltered = json.data.recordsFiltered;
                return json.data.data;
            },
            url:'/sysRole/getSysButtonListByRoleMenuId',
            type:'post',
            "data": {
                "menuId": treeNode.smId,
                "roleId": $("#roleId").val()
            }
        },
        columns: [
            { data: null,title: "",
                render: function(data, type, row, meta) {
                return '<input class="role-switch" type="checkbox"  '+data.checked+'  value="' + row.sbId + '"/>'}
            },
            { data: 'buttonName' ,title:"按钮名称"},
            { data: 'buttonMethod' ,title:"按钮方法"},
            { data: 'description' ,title:"描述"}
        ],
       drawCallback: function( settings ) {
           $('.role-switch').bootstrapSwitch({    //初始化按钮
               onText:"启用中",
               offText:"关闭中",
               onColor:"success",
               offColor:"info",
               size:"small",
               onSwitchChange:function(event,state){
                   var roleId=$("#roleId").val();
                   var menuId=addAuthSmId;
                   var buttonId=event.target.defaultValue;
                   $(this).bootstrapSwitch('state', !state, true);
                   $(this).bootstrapSwitch('disabled', true);
                   var _this=$(this);
                   $.post("/sysRole/switchButtonAuth",{roleId:roleId,menuId:menuId,buttonId:buttonId},function(result){
                       if(result){
                           if (result['status']===0){
                               _this.bootstrapSwitch('disabled', false);
                               _this.bootstrapSwitch('toggleState',true);
                           }else {
                               _this.bootstrapSwitch('disabled', false);
                           }
                       }else{
                           _this.bootstrapSwitch('disabled', false);
                       }
                   }).error(function() { _this.bootstrapSwitch('disabled', false); });
               }
           });
       }
    });

}
